
Promise.race2 = (promises) => {
    return new Promise((resolve, reject) => {
        promises.forEach((promise, index) => {
            // 对p进行一次包装，防止非Promise对象
            Promise.resolve(promise).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        });
    })
}

// 测试
const p1 = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve(1)
    }, 1000)
})
const p2 = Promise.resolve(2)
const p3 = 3
const p4 = new Date()
const p5 = Promise.reject(111)
const promises = [p1, p2, p3, p4, p5]

Promise.race(promises).then(res => {
    console.log(res)
})

Promise.race2(promises).then(res => {
    console.log(res)
})